package com._21cn._front.product.page.label;

import com._21cn.cms.common.Constants;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.utils.StringUtil;
import com._21cn.product.entity.Car;
import com._21cn.product.entity.CarBusinessPledge;
import com._21cn.product.entity.CarBusinessSecond;
import com._21cn.product.entity.Cate;
import com._21cn.product.service.CarBusinessPledgeService;
import com._21cn.product.service.CarBusinessSecondService;
import com._21cn.product.service.CarService;
import com._21cn.product.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/4.
 */
@Component
public class CarLabel {

    @Autowired(required = false)
    private CarBusinessPledgeService carBusinessPledgeService;

    @Autowired(required = false)
    private CarBusinessSecondService carBusinessSecondService;

    @Autowired(required = false)
    private CarService carService;

    @Autowired(required = false)
    private CateService CateService;

    /**
     * 获取抵押车列表
     * @param brandId
     * @param seriesId
     * @param otherId
     * @param gtPrice
     * @param ltPrice
     * @param sortRank
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageList getPledgeCarList(String brandId, String seriesId, String otherId, String gtPrice, String ltPrice, String sortRank,
                                     String pageNo, String pageSize){
        return getPledgeCarList(brandId, seriesId, otherId, gtPrice, ltPrice, null, sortRank, pageNo, pageSize);
    }

    public PageList getPledgeCarList(String brandId, String seriesId, String otherId,
                                        String gtPrice, String ltPrice, String keyword, String sortRank,
                                            String pageNo, String pageSize){
        Map<String, Object> params = new HashMap<String, Object>();

        if(StringUtil.isNumber(brandId) && Integer.valueOf(brandId)>0){
            params.put("eqBrandId",brandId);
        }

        if(StringUtil.isNumber(seriesId) && Integer.valueOf(seriesId)>0){
            params.put("eqSeriesId",seriesId);
        }

        if(StringUtil.isNumber(otherId) && Integer.valueOf(otherId)>0){
            params.put("eqOtherId",otherId);
        }

        if(StringUtil.isNumber(gtPrice) && Integer.valueOf(gtPrice)>0){
            params.put("gtPrice",gtPrice);
        }

        if(StringUtil.isNumber(ltPrice) && Integer.valueOf(ltPrice)>0){
            params.put("ltPrice",ltPrice);
        }

        if(!StringUtil.isEmpty(keyword)){
            params.put("likeCarName", keyword);
        }

        if(("priceDesc").equals(sortRank)){         //价格排序
            params.put("orderByClause", "a.SECOND_PRICE DESC");
        }else if(("priceAsc").equals(sortRank)){
            params.put("orderByClause", "a.SECOND_PRICE ASC");
        }

        if(("createTimeDesc").equals(sortRank)){    //创建排序
            params.put("orderByClause", "a.CREATE_TIME DESC");
        }else if(("createTimeAsc").equals(sortRank)){
            params.put("orderByClause", "a.CREATE_TIME ASC");
        }

        if(("odoMeterDesc").equals(sortRank)){      //里程排序
            params.put("orderByClause", "a.ODO_METER DESC");
        }else if(("odoMeterAsc").equals(sortRank)){
            params.put("orderByClause", "a.ODO_METER ASC");
        }

        if(("boardTimeDesc").equals(sortRank)){      //车龄排序
            params.put("orderByClause", "a.DATE_ON_BOARD DESC");
        }else if(("boardTimeAsc").equals(sortRank)){
            params.put("orderByClause", "a.DATE_ON_BOARD ASC");
        }

        PageList<CarBusinessPledge> list = carBusinessPledgeService.getCarPledgeList(params, Integer.valueOf(pageNo), Integer.valueOf(pageSize), true);
        for(CarBusinessPledge carPledge : list){
            Integer carId = carPledge.getCarId();
            Car car = carService.getById(carId);
            if( car!=null ){
                carPledge.initCarPledge(car);
            }
        }
        return list;
    }

    /**
     * 获取二手车列表
     * @param brandId
     * @param seriesId
     * @param otherId
     * @param gtPrice
     * @param ltPrice
     * @param sortRank
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageList getSecondCarList(String brandId, String seriesId, String otherId, String gtPrice, String ltPrice, String sortRank,
                                     String pageNo, String pageSize){
        return getSecondCarList(brandId, seriesId, otherId, gtPrice, ltPrice, null, sortRank, pageNo, pageSize);
    }

    public PageList getSecondCarList(String brandId, String seriesId, String otherId,
                                     String gtPrice, String ltPrice, String keyword, String sortRank,
                                     String pageNo, String pageSize){
        Map<String, Object> params = new HashMap<String, Object>();

        if(StringUtil.isNumber(brandId) && Integer.valueOf(brandId)>0){
            params.put("eqBrandId",brandId);
        }

        if(StringUtil.isNumber(seriesId) && Integer.valueOf(seriesId)>0){
            params.put("eqSeriesId",seriesId);
        }

        if(StringUtil.isNumber(otherId) && Integer.valueOf(otherId)>0){
            params.put("eqOtherId",otherId);
        }

        if(StringUtil.isNumber(gtPrice) && Integer.valueOf(gtPrice)>0){
            params.put("gtPrice",gtPrice);
        }

        if(StringUtil.isNumber(ltPrice) && Integer.valueOf(ltPrice)>0){
            params.put("ltPrice",ltPrice);
        }

        if(!StringUtil.isEmpty(keyword)){
            params.put("likeCarName", keyword);
        }

        if(("priceDesc").equals(sortRank)){         //价格排序
            params.put("orderByClause", "a.SECOND_PRICE DESC");
        }else if(("priceAsc").equals(sortRank)){
            params.put("orderByClause", "a.SECOND_PRICE ASC");
        }

        if(("createTimeDesc").equals(sortRank)){    //创建排序
            params.put("orderByClause", "a.CREATE_TIME DESC");
        }else if(("createTimeAsc").equals(sortRank)){
            params.put("orderByClause", "a.CREATE_TIME ASC");
        }

        if(("odoMeterDesc").equals(sortRank)){      //里程排序
            params.put("orderByClause", "a.ODO_METER DESC");
        }else if(("odoMeterAsc").equals(sortRank)){
            params.put("orderByClause", "a.ODO_METER ASC");
        }

        if(("boardTimeDesc").equals(sortRank)){      //车龄排序
            params.put("orderByClause", "a.DATE_ON_BOARD DESC");
        }else if(("boardTimeAsc").equals(sortRank)){
            params.put("orderByClause", "a.DATE_ON_BOARD ASC");
        }
        params.put("eqStatus", Constants.STATUS_ENABLE);
        PageList<CarBusinessSecond> list = carBusinessSecondService.getCarSecondList(params, Integer.valueOf(pageNo), Integer.valueOf(pageSize), true);
        for(CarBusinessSecond carSecond : list){
            Integer carId = carSecond.getCarId();
            Car car = carService.getById(carId);
            if( car!=null ){
                carSecond.initCarPledge(car);
            }
        }

        return list;
    }

    /**
     * 获取推荐品牌
     * @return
     */
    public List getBrandList4Show(){
        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("eqIsShow", Constants.IS_SHOW);
        params.put("eqTypeId", Constants.CATE_TYPE_BRAND);
        params.put("eqStatus", Constants.STATUS_ENABLE);
        params.put("orderByClause", "SORT_NUM ASC");
        List<Cate> brandList = CateService.getList(params, 1, Constants.DEFAULT_PAGE_SIZE_COMMON);
        return brandList;
    }

    /**
     * 获取推荐车系
     * @return
     */
    public List getSeriesList4Show(){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqIsShow", Constants.IS_SHOW);
        params.put("eqTypeId", Constants.CATE_TYPE_SERIES);
        params.put("eqStatus", Constants.STATUS_ENABLE);
        List<Cate> brandList = CateService.getList(params, 1, Constants.DEFAULT_PAGE_SIZE_COMMON);
        return brandList;
    }

    /**
     * 获取类型
     * @return
     */
    public List getOtherList4Show(){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqTypeId", Constants.CATE_TYPE_OTHER);
        params.put("eqStatus", Constants.STATUS_ENABLE);
        List<Cate> brandList = CateService.getList(params, 1, Constants.DEFAULT_PAGE_SIZE_COMMON);
        return brandList;
    }

    public CarBusinessPledge getPledgeCar(String idStr){
        Integer id = Integer.valueOf(idStr);
        CarBusinessPledge carPledge = carBusinessPledgeService.getById(id);
        if(carPledge!=null){
            Integer carId = carPledge.getCarId();
            Car car = carService.getById(carId);
            if( car!=null ){
                carPledge = carPledge.initCarPledge(car);
            }
        }
        return carPledge;
    }

    public CarBusinessSecond getSecondCar(String idStr){
        Integer id = Integer.valueOf(idStr);
        CarBusinessSecond carSecond = carBusinessSecondService.getById(id);
        if(carSecond!=null){
            Integer carId = carSecond.getCarId();
            Car car = carService.getById(carId);
            if( car!=null ){
                carSecond = carSecond.initCarPledge(car);
            }
        }
        return carSecond;
    }

}
